<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="applicationRule" :model="applicationForm" label-position="left" label-width="140px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="applicationForm.title" />
      </el-form-item>
      <el-form-item label="发行人" prop="author">
        <el-input v-model="applicationForm.author" />
      </el-form-item>
      <el-form-item label="图标">
        <Upload v-model="applicationForm.avatar" />
      </el-form-item>
      <el-form-item label="规则">
        <el-input v-model="applicationForm.content" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="优惠卷规则" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="applicationForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="applicationForm.status.value === 5" type="danger" @click="handleDelete(applicationForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveApplication, removeApplicationById, getApplication } from '@/api/program/application'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Upload from '@/components/Upload/Avatar' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Upload },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      applicationForm: {
        title: '',
        avatar: '',
        author: '',
        status: {
          label: '',
          value: 1
        },
        remark: '',
        content: ''
      },
      applicationRule: []
    }
  },
  created() {
    let applicationId = this.$route.query && this.$route.query.applicationId
    if (applicationId !== undefined) {
      applicationId = parseInt(applicationId)
      if (!isNaN(applicationId)) {
        this.listLoading = true
        const formData = {
          'applicationId': applicationId
        }
        getApplication(formData).then(response => {
          if (response.status === 200) {
            this.applicationForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(applicationId)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'application' })
    },
    handleResetForm() {
      this.applicationForm = {
        title: '',
        avatar: '',
        status: {
          label: '',
          value: 1
        },
        remark: '',
        content: ''
      }
    },
    createData() {
      const _applicationForm = this.applicationForm
      // return false;
      _applicationForm.status = 1
      if (_applicationForm.identity > 0) {
        _applicationForm.applicationId = _applicationForm.identity
      }
      console.log(this.applicationForm)
      saveApplication(_applicationForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'application' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row) {
      const _this = this
      const formData = {
        'applicationId': row.identity
      }
      console.log(row)
      removeApplicationById(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          _this.$router.push({ path: 'application' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
